#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:do_exam1.py
# author:王雯雯
# datetime:2024-11-12 
# software: PyCharm
import random
from dog import Dog
from person import Person


class Fight:
    def __init__(self):

        self.people = [Person(), Person()]

        self.dogs = [Dog(), Dog(), Dog()]

    def start_fight(self):

        while True:
            # 随机决定谁先攻击
            from random import random
            if random.random()< 0.5:
                attacker, defender = self.choose_attacker_and_defender(self.people, self.dogs)
            else:
                attacker, defender = self.choose_attacker_and_defender(self.dogs, self.people)

            if isinstance(attacker, Person):
                attacker.hit(defender)
            else:
                attacker.bite(defender)

            # 检查是否有一方全部死亡
            if self.all_dead(self.people):
                print("狗胜利！")
                break
            elif self.all_dead(self.dogs):
                print("人胜利！")
                break

    @staticmethod
    def choose_attacker_and_defender(attackers, defenders):

        from random import random
        attacker = random.choice(attackers)
        alive_defenders = [d for d in defenders if d.health > 0]
        defender = random.choice(alive_defenders)
        return attacker, defender

    @staticmethod
    def all_dead(entities):

        return all(entity.health == 0 for entity in entities)
fight = Fight()
fight.start_fight()